//
//  SignForgetPdViewController.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/13.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "SignForgetPdViewController.h"
#import "IQKeyboardManager.h"
@interface SignForgetPdViewController ()
//{
//    AFHTTPRequestOperation *workingOperation;
//}
@end

@implementation SignForgetPdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupView];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleNotification:)
                                                 name:SVProgressHUDDidTouchDownInsideNotification
                                               object:nil];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SVProgressHUDDidTouchDownInsideNotification object:nil];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.operationQueue cancelAllOperations];
}
#pragma mark 点击取消等待界面
- (void)handleNotification:(NSNotification *)notif
{
    [SVProgressHUD dismiss];
    [self.workingOperation cancel];
}
-(void)setupView
{
    
//    [super titleWithLabel:@"重置密码" andColor:nil];
    self.title = @"重置密码";
    
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];
    [rightBtn setTitle:@"完成" forState:0];
    [rightBtn addTarget:self action:@selector(finishAction) forControlEvents:64];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];
    [leftBtn setTitle:@"返回" forState:0];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:64];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelEdit)];
    [self.view addGestureRecognizer:tap];
    
    
    self.telField.layer.borderWidth = 0.3;
    self.telField.layer.cornerRadius = 5;
    self.telField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.idField.layer.borderWidth = 0.3;
    self.idField.layer.cornerRadius = 5;
    self.idField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.pdField.layer.borderWidth = 0.3;
    self.confirmField.layer.cornerRadius = 5;
    self.confirmField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
}
-(void)cancelEdit
{
    [self.telField resignFirstResponder];
    [self.idField resignFirstResponder];
}
-(void)finishAction
{
    
    if ([self isRight]) {
        //    13690699669
        //    440682199110195059
        [SVProgressHUD showWithStatus:@"正在提交" maskType:SVProgressHUDMaskTypeBlack];
        NSString *telNum =[[BuaatVolunteerServerSDK sharedInstance]encryptStrWithPublicKey: self.telField.text];
        NSString *idNum =self.idField.text;
        [[BuaatVolunteerServerSDK sharedInstance]requestVolunteerForgetVolunteerPwdWithLoginMobile:telNum last6IdNo:idNum success:^(NSHTTPURLResponse *response, id responseObject) {
            [SVProgressHUD dismiss];
            NSNumber *num = responseObject[@"ret"];
            if ([num isKindOfClass:[NSNumber class]]) {
                                if (num.intValue==0) {
                                    [self getRight];
                                }else if(num.intValue==1101)
                                {
                                    [SVProgressHUD showErrorWithStatus:@"手机号还没注册" maskType:SVProgressHUDMaskTypeBlack];

                                }else
                                {
                                    [SVProgressHUD showErrorWithStatus:@"身份证不对" maskType:SVProgressHUDMaskTypeBlack];
                                }
            }
        } failure:^(NSHTTPURLResponse *response, NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
        }];
        
//        NSDictionary *parameters = @{@"token":@"sdfsadfsadfasdf",@"module":@"Volunteer",@"method":@"forgotPwd",@"requestId":[NSNull new],@"requestParam":@[self.telField.text,self.idField.text]};
//        
//        self.workingOperation = [Utils requestDic:parameters andRusult:^(id result) {
//            NSNumber *num = result[@"responseData"];
//            NSLog(@"clas = %@",[num class]);
//            if ([num isKindOfClass:[NSNumber class]]) {
//                if (num.intValue==0) {
//                    [self getRight];
//                }else if(num.intValue==-1)
//                {
//                    [SVProgressHUD showErrorWithStatus:@"手机号还没注册" maskType:SVProgressHUDMaskTypeBlack];
//                    
//                }else
//                {
//                    [SVProgressHUD showErrorWithStatus:@"身份证不对" maskType:SVProgressHUDMaskTypeBlack];
//                }
//            }
//        } andError:^(id result) {
//            [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
//            
//        }];
        
        
       
    }
    
}
-(void)getRight
{
    [SVProgressHUD showSuccessWithStatus:@"修改成功" maskType:SVProgressHUDMaskTypeBlack];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    });
}
-(BOOL)isRight
{
    if (![Utils validateMobile:self.telField.text]) {
        [SVProgressHUD showErrorWithStatus:@"手机号码格式错误" maskType:SVProgressHUDMaskTypeBlack];
        
        return 0;
    }
    if (self.idField.text.length<6) {
        [SVProgressHUD showErrorWithStatus:@"请输入身份证后6位" maskType:SVProgressHUDMaskTypeBlack];
        return 0;
    }
    return 1;
}
-(void)backAction
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
